from tortoise import fields

from .base import BaseModel


class Role(BaseModel):
    """Role model for RBAC system."""

    name = fields.CharField(max_length=50, unique=True)
    description = fields.TextField(null=True)
    parent_role = fields.ForeignKeyField(
        "models.Role",
        related_name="child_roles",
        null=True,
        description="Parent role for hierarchical structure")

    # Reverse relations
    permissions = fields.ManyToManyField(
        "models.Permission",
        through="role_permissions",
        related_name="roles",
        description="Permissions assigned to this role")

    class Meta:
        table = "roles"

    def __str__(self):
        return self.name
